<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@include file="../common/head.jsp" %>
<div class="right_col" role="main">
    <form class="form-horizontal form-label-left" method="post" action="/admin/flatform/commodity/insertCommodity"
          onsubmit="return checkall()">
        <span class="section">增加商品信息</span>
        <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-5">商品名称<span>*</span>
            </label>
            <div class="col-md-6 col-sm-6 col-xs-5">
                <input id="comName" name="comName" class="form-control col-md-7 col-xs-5" value="${commodity.comName}"
                       type="text" onblur="checkcomName()"><span id="demo1" style="color: red;"></span>
            </div>
        </div>
        <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-5">进价 <span>*</span>
            </label>
            <div class="col-md-6 col-sm-6 col-xs-5">
                <input id="imPrice" name="imPrice" class="form-control col-md-7 col-xs-5" value="${commodity.imPrice}"
                       type="text" onblur="ischeckimPrice()"> <span id="demo3" style="color: red;"></span>
            </div>
        </div>
        <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-5">售价 <span>*</span>
            </label>
            <div class="col-md-6 col-sm-6 col-xs-5">
                <input id="price" name="price" class="form-control col-md-7 col-xs-5" value="${commodity.price}"
                       type="text" onblur="ischeckNum()"><span id="demo2" style="color: red;"></span>
            </div>
        </div>
        <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-5">生产日期<span>*</span>
            </label>
            <div class="col-md-6 col-sm-6 col-xs-5">
                <input id="comDate" name="comDate" class="form-control col-md-7 col-xs-5"
                       value="<fmt:formatDate pattern="yyyy-MM-dd" value="${commodity.comDate}"/>" type="date" required="" max="">
            </div>
        </div>
        <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-5">过期时间<span>*</span>
            </label>
            <div class="col-md-6 col-sm-6 col-xs-5">
                <input id="outDate" name="outDate" class="form-control col-md-7 col-xs-5"
                       value="<fmt:formatDate pattern="yyyy-MM-dd" value="${commodity.outDate}"/>" type="date"
                       required="" min=""/>
            </div>
        </div>
        <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-5">供应商名称<span>*</span>
            </label>
            <div class="col-md-6 col-sm-6 col-xs-5">
                <input id="supName" name="supName" class="form-control col-md-7 col-xs-5" value="${commodity.supName}"
                       type="text" required="">
            </div>
        </div>
        <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-5">商品类型<span>*</span>
            </label>
            <div class="col-md-6 col-sm-6 col-xs-5">
                <input id="comType" name="comType" class="form-control col-md-7 col-xs-5" value="${commodity.comType}"
                       type="text" required="">
            </div>
        </div>
        <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-5">计件方式<span></span>
            </label>
            <div class="col-md-6 col-sm-6 col-xs-5">
                <input id="unit" name="unit" class="form-control col-md-7 col-xs-5" value="${commodity.unit}"
                       type="text">
            </div>
        </div>
        <a onclick="javascript:history.back(-1);" class=" text-left btn btn-default  " role="button">返回上一页</a>
        <button type="submit" class="btn btn-primary text-center  col-md-offset-4 col-lg-offset-4col-xl-offset-4">增加
        </button>
        <button class="btn btn-primary text-center" onclick="reset()">重置</button>
    </form>
    <div class="clearfix"></div>
</div>
<%@include file="../common/footer.jsp" %>
<script>
    /*  校验商品名称*/
    function check() {
        var name = document.getElementById('comName').value;
        if (name) {
            document.getElementById("demo1").innerHTML = " ";
            return true;
        } else {
            document.getElementById("demo1").innerHTML = "请输入商品名称！";
            document.getElementById('comName').focus();
            return false;
        }
    }

    /*判断输入的价格是不是数字  */
    function ischeckNum() {
        var num = document.getElementById('price').value;
        if (num) {
            if (!isNaN(num)) {
                document.getElementById("demo2").innerHTML = " ";
                return true;
            } else {
                document.getElementById("demo2").innerHTML = "你输入的数据不是数字！";
                document.getElementById('price').value = "";
                document.getElementById('price').focus();
                return false;
            }
        } else {
            document.getElementById("demo2").innerHTML = "请输入商品售价！";
            document.getElementById('price').focus();
            return false;
        }
    }

    function ischeckimPrice() {
        var num = document.getElementById('imPrice').value;
        if (num) {
            if (!isNaN(num)) {
                document.getElementById("demo3").innerHTML = " ";
                return true;
            } else {
                document.getElementById("demo3").innerHTML = "你输入的数据不是数字！";
                document.getElementById('imPrice').value = "";
                document.getElementById('imPrice').focus();
                return false;
            }
        } else {
            document.getElementById("demo3").innerHTML = "请输入商品进价！";
            document.getElementById('imPrice').focus();
            return false;
        }
    }

    /* 校验整个表单 */
    function checkall() {
        var name = checkcomName();
        var price = ischeckNum();
        var imPrice = ischeckimPrice();
        if (price && name && imPrice) {
            alert("添加成功");
            return true;
        } else {
            alert("添加失败，请重新填写数据");
            reset();
            return false;
        }
    }

    function reset() {
        window.location.href = "${pageContext.request.contextPath }/admin/flatform/commodity/toAdd";
    }

    $("#comDate").on("click", function () {
        //当前时间
        var date_now = new Date();
        //得到当前年份
        var year = date_now.getFullYear();
        //得到当前月份
        // js中获取Date中的month时，会比当前月份少一个月，所以这里需要先加1
        // 判断当前月份是否小于10，如果小于，那么就在月份的前面加一个 '0' ， 如果大于，就显示当前月份
        var month = date_now.getMonth() + 1 < 10 ? "0" + (date_now.getMonth() + 1) : (date_now.getMonth() + 1);
        //得到当前日期
        var date = date_now.getDate() < 10 ? "0" + date_now.getDate() : date_now.getDate();
        //设置input标签的max属性
        $("#comDate").attr("max", year + "-" + month + "-" + date);
        document.getElementById('outDate').value = "";
    })
    $("#outDate").on("click", function () {
        var oldDate = document.getElementById('comDate').value;
        //设置input标签的max属性
        $("#outDate").attr("min", oldDate);
    })
</script>
